package cn.schoolwow.workflow.test.module.oa.common.flow.common;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.deploy.DeployNodeType;
import com.alibaba.fastjson.JSONObject;

public class ConvertAndSetDeployNodeTypeCommonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        JSONObject childNode = flowContext.checkData("childNode", JSONObject.class);

        flowContext.executeFlowList(new SetConvertWorkFlowNodeNameCommonFlow());
        String name = flowContext.checkData("nodeName", String.class);
        int type = childNode.getIntValue("type");

        DeployNodeType deployNodeType = null;
        switch (type){
            case 0:{
                deployNodeType = DeployNodeType.StartNode;
            }break;
            case 1:{
                //判断approvalType,0:人工审核,1:自动通过,2:自动拒绝
                int approvalType = childNode.getIntValue("approvalType");
                switch (approvalType){
                    case 0:{deployNodeType = DeployNodeType.AuditNode;}break;
                    case 1:{deployNodeType = DeployNodeType.PassNode;}break;
                    case 2:{deployNodeType = DeployNodeType.RejectNode;}break;
                    default:{
                        throw new IllegalArgumentException("审批类型值不合法!允许值[0,1,2],当前值:["+approvalType+"],节点名称:"+name);
                    }
                }
            }break;
            case 2:{
                deployNodeType = DeployNodeType.CarbonCopyNode;
            }break;
            case 3:{
                deployNodeType = DeployNodeType.ConditionNode;
            }break;
            case 4:{
                deployNodeType = DeployNodeType.ExclusiveGateway;
            }break;
            case 5:{
                throw new IllegalArgumentException("不支持办理节点!节点名称:"+name);
            }
            case 6:{
                throw new IllegalArgumentException("不支持触发器节点!节点名称:"+name);
            }
            case 9:{
                deployNodeType = DeployNodeType.EndNode;
            }break;
            default:{
                throw new IllegalArgumentException("不支持的节点类型!节点值:"+type);
            }
        }
        flowContext.putTemporaryInstanceData(deployNodeType);
    }

    @Override
    public String name() {
        return "转换部署节点类型";
    }
}
